<template>
  <div>
    <BaseSearch :config="searchConfig" v-bind="form" ref="search"></BaseSearch>
    <BaseTable :columns="columns" v-bind="table"></BaseTable>
  </div>
</template>

<script>
import BaseSearch from '@/components/BaseSearch/index.vue'
import BaseTable from '@/components/BaseTable/index.vue'
import { FormTableMixin } from '@/mixins/form-table'
import http from '@/utils/request'

const service = async params => {
  const { records, total } = await http.get('/ents', {
    params: params,
  })

  return {
    data: records,
    total: total,
  }
}

export default {
  name: 'infoList',
  components: { BaseSearch, BaseTable },
  mixins: [FormTableMixin(service)],
  data() {
    const isCanDetail = this.$checkAccess('MENU_ENTERPRISE_INFO_DETAIL')
    return {
      searchConfig: [
        { component: 'input', prop: 'searchStr', label: '搜索', placeholder: '企业名称/统一信用代码' },
        {
          component: 'select',
          prop: 'isAuth',
          label: '实名认证',
          options: [
            { label: '是', value: '1' },
            { label: '否', value: '0' },
          ],
        },
        { component: 'select', prop: 'appCode', label: '来源', options: [] },
      ],
      columns: [
        { label: '企业名称', prop: 'tenantName' },
        { label: '统一信用代码', prop: 'uscCode' },
        { label: '法定代表人', prop: 'legalPerson' },
        { label: '联系人', prop: 'contactsIdentificationName' },
        { label: '联系人方式', prop: 'contactsIdentificationNumber' },
        {
          label: '实名认证',
          prop: 'isAuth',
          render: (value, row) => {
            return <div>{row.isAuth === '1' ? '是' : '否'}</div>
          },
        },
        { label: '所属项目', prop: 'appName' },
        {
          label: '操作',
          render: (val, row) => {
            return (
              <div>
                {isCanDetail && (
                  <el-button
                    type="text"
                    onClick={() => {
                      this.goDetail(row)
                    }}>
                    详情
                  </el-button>
                )}
                {isCanDetail && (
                  <el-button
                    type="text"
                    onClick={() => {
                      this.updateStatus(row)
                    }}>
                    {row.status === '1' ? '禁用' : '启用'}
                  </el-button>
                )}
              </div>
            )
          },
        },
      ],
    }
  },
  methods: {
    async getDataList() {
      const res = await this.$http.get('/ents?current=1&limit=15')
      this.table = {
        data: res.records,
        total: res.total,
      }
    },
    goDetail(item) {
      this.$router.push({
        name: 'companyDetail',
        query: {
          companyId: item.id,
          appCode: item.appCode,
          applyId: item.id,
          uscCode: item.uscCode || '',
          isFromEnterprise: 1,
        },
      })
    },
    updateStatus(data) {
      // 1 启用 0 禁用
      const msgText = data.status === '0' ? '启用' : '禁用'
      this.$confirm(`此操作将${msgText}, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          const state = data.status === '0' ? '1' : '0'
          const obj = {
            type: state,
            uscCode: data.uscCode,
          }
          await this.$http.post('/ents/updateUserstatus', obj)
          this.getDataList()
        })
        .catch(() => {
          // this.$message({
          //   type: 'info',
          //   message: '已取消删除',
          // })
        })

      // this.getDataList()
      // this.$refs.search.$refs.searchForm.resetFields()
      // this.form.onSubmit()
    },
    //获取来源
    async getSourceListRequest() {
      const sourceList = await this.$http.get(`/app/list`)
      if (sourceList && sourceList.length) {
        const list = sourceList.map(item => {
          return {
            label: item.name,
            value: item.code,
          }
        })
        this.searchConfig[2].options = [...list]
      }
    },
  },
  mounted() {
    this.getSourceListRequest()
  },
}
</script>

<style lang="scss" scoped></style>
